;
(function () {
    $(function () {
        $.idcode.setCode();//生成验证码方法

        //定义自己的规则
        $.validator.addMethod("checkPhone", (value, element, param) => {
            var reg = /^[1][3-9]\d{9}$/i;
            return reg.test(value);
        })

        //给表单绑定事件
        $("form").validate({
            //规则
            rules: {
                u_phone: {
                    checkPhone: true,
                    required: true, //必填
                },
                u_pwd: {
                    required: true, //必填
                    rangelength: [6, 16],
                },
                u_tpwd: {
                    equalTo: "#upwd"
                },
            },
            //提示
            messages: {
                u_pwd: {
                    required: '密码必填',
                    rangelength: "密码必须是{0}-{1}位"
                },
                u_tpwd: {
                    equalTo: "两次密码不一致"
                },
                u_phone: {
                    required: '手机号必填',
                    checkPhone: "手机号不合法"
                },
            },
            //提交
            submitHandler: function () {
                var IsBy = $.idcode.validateCode();
                if (!IsBy) {
                    layer.msg('验证码错误!');
                    return false;
                } else {
                    var formdata = new FormData();
                    formdata.append("u_phone", $('[name=u_phone]').val())
                    formdata.append("u_pwd", $('[name=u_pwd]').val());
                    //使用ajax传递参数
                    $.ajax({
                        url: "./api/reg",
                        data: formdata,
                        type: "post",
                        processData: false,
                        contentType: false,
                    }).done(function (res) {
                        layer.confirm("注册成功!是否立即去登录?", {
                            btn: ["确定", "取消"]
                        }, (index) => {
                            layer.close(index);
                            location = "./login.html";
                        }, () => {

                        })
                    })
                }
                return false;
            }
        })
        $("#hyz").on("click", function () {
            $.idcode.setCode();
        })
    })


})()